/*
 * @Author: Hp_lucky 1063512385@qq.com
 * @Date: 2024-10-08 11:09:30
 * @LastEditors: Hp_lucky 1063512385@qq.com
 * @LastEditTime: 2024-10-17 15:48:22
 * @FilePath: \lucky-rpg\assets\script\src\Basic\Model\Platform\H5Platform.ts
 */
/* eslint-disable */
import { _decorator } from 'cc';
import { AdType } from '../../../Data/Types';
import PlatformBase from './PlatformBase';
import { CallBackHandler } from '../PoolObj/CallBackHandler';
import { HandlerManager } from '../../Handle/HandlerManager';
import { HandleCode } from '../../Handle/Handler';
const { ccclass } = _decorator;

@ccclass
/**
 * 多平台抽象类
 */
export class H5Platform extends PlatformBase {
  login(callback?: CallBackHandler) {
    HandlerManager.Ins().handle(HandleCode.MAIN_SIGNIN, 0, callback);
  }
  startShowVideo() {
    this.showVideoSuccess();
    this.adFinish();
  }
  /** http请求 */
  requestHttpGet(
    url: string,
    data: any,
    callback: CallBackHandler,
    headers: { [key: string]: string } = {
      'Content-Type': 'application/json',
    }
  ) {
    if (data) {
      url += '?';
      for (const key in data) {
        url += `${key}=${data[key]}&`;
      }
      url = url.slice(0, -1);
    }
    fetch(url, {
      method: 'GET',
      headers: headers,
    })
      .then((response) => response.text())
      .then((result) => {
        try {
          result = JSON.parse(result);
        } catch {
          result = null;
        }
        callback?.execute(result);
      })
      .catch((error) => console.log('error', error));
  }

  /** http请求 */
  requestHttpPost(
    url: string,
    data: any,
    callback: CallBackHandler,
    headers: { [key: string]: string } = {
      'Content-Type': 'application/json',
    }
  ) {
    fetch(url, {
      method: 'POST',
      headers: headers ? headers : {},
      body: JSON.stringify(data),
    })
      .then((response) => response.text())
      .then((result) => {
        try {
          result = JSON.parse(result);
        } catch {
          result = null;
        }
        callback?.execute(result);
      })
      .catch((error) => console.log('error', error));
  }

  notifyMiniProgramPlayableStatus() {}
}
